/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean;

import com.entities.Magasin;
import com.entities.Produit;
import com.entities.Vendeur;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author mathieumiollan
 */
@Stateless
@LocalBean
public class GestionnaireDeMagasin {

    @PersistenceContext(unitName = "EMall-ejbPU")
    private EntityManager em;

    public void ajouterProduit(Magasin m, Produit p) {
        p.setMagasin(m);
        em.merge(p);
        m.ajouterProduit(p);
        em.merge(m);
    }
    
    public void ajouterProprio(Magasin m, Vendeur v){
        m.setProprio(v);
        v.setMagasin(m);
        em.merge(m);
        em.merge(v);
    }

    public List<Produit> getProduitSearch(String vendeur, String typerecherche, String req, String categorie, String distance, int first, int size, String sortField, String sortOrder) {
        if(sortField == null){
            sortField = "nom";
        }
        String order = "DESC";
        if(sortOrder == "ASCENDING") {
            order = "ASC";
        }

        if(typerecherche == null || typerecherche.contentEquals("")) {
            if (req == null || req.contentEquals("")) {
                Query q;
                if (categorie == null || categorie.contentEquals("")){
                    q = em.createQuery("SELECT p FROM Produit p ORDER BY p."+sortField+" "+order);
                }
                else {
                    q = em.createQuery("select p from Produit p WHERE p.categorie='"+categorie+"' ORDER BY p."+sortField+" "+order);
                }
                q.setMaxResults(size);
                q.setFirstResult(first);
                return q.getResultList();
            } else {
                Query q;
                if (categorie == null || categorie.contentEquals("")){
                    q = em.createQuery("select p from Produit p where upper(p.description) like upper('%" + req + "%') OR upper(p.nom) like UPPER('%" + req + "%') ORDER BY p."+sortField+" "+order);
                }
                else {
                    q = em.createQuery("select p from Produit p where upper(p.description) like upper('%" + req + "%') OR upper(p.nom) like UPPER('%" + req + "%') AND p.categorie='"+categorie+"' ORDER BY p."+sortField+" "+order);
                }
                q.setMaxResults(size);
                q.setFirstResult(first);
                return q.getResultList();
            }
        }
        else {
            if (vendeur == null || vendeur.contentEquals("")) {
                Query q;
                 q = em.createQuery("SELECT p FROM Produit p ORDER BY p."+sortField+" "+order);
                q.setMaxResults(size);
                q.setFirstResult(first);
                return q.getResultList();
            }
            else {
                Query q;
                q = em.createQuery("SELECT p FROM Produit p WHERE upper(p.magasin.proprio.nom) like upper('%" + vendeur + "%') ORDER BY p."+sortField+" "+order);
                q.setMaxResults(size);
                q.setFirstResult(first);
                return q.getResultList();
            }
        }
    }

    public void supprimerProduit(Magasin m, Produit p) {
        m.supprimerProduit(p);
        em.merge(m);
    }

    public void modifierProduit(Produit p) {
        em.merge(p);
    }
}
